#include "frm_userpage.h"
#include "ui_frm_userpage.h"

frm_UserPage::frm_UserPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::frm_UserPage)
{
    ui->setupUi(this);
    QPixmap t(":/res/icons/df.ico");
    this->ui->label->setPixmap(t);
    initTree();
    initStack();
    ui->stackedWidget->setCurrentIndex(0);
    connect(ui->treeWidget,&QTreeWidget::itemClicked,this,&frm_UserPage::itemclicked);
}

frm_UserPage::~frm_UserPage()
{
    delete ui;
}
void frm_UserPage::initTree(){
    ui->treeWidget->clear();
    QTreeWidgetItem* t1 = new QTreeWidgetItem(frm_UserPage::itTopitem);
    t1->setText(0,"个人中心");
    getItem(t1,"个人信息");
    getItem(t1,"账号安全");
    getItem(t1,"手机绑定");
    getItem(t1,"乘客列表");
    QTreeWidgetItem* t2 = new QTreeWidgetItem(frm_UserPage::itTopitem);
    t2->setText(0,"订单中心");
    ui->treeWidget->addTopLevelItem(t1);
    ui->treeWidget->addTopLevelItem(t2);
    //ui->treeWidget->setCurrentItem(getItem(t1,"个人信息"));
}
void frm_UserPage::initStack(){
    ui->stackedWidget->removeWidget(ui->stackedWidget->widget(0));
    ui->stackedWidget->removeWidget(ui->stackedWidget->widget(0));
    PersonalCenter* g = new PersonalCenter();
    connect(g,&PersonalCenter::changeUserData,this,frm_UserPage::changeUserData);

    QWidget* waiting = new QWidget(this);
    QLabel* lb = new QLabel(waiting);
    lb->setText("加载中...");
    ui->stackedWidget->addWidget(waiting);
    ui->stackedWidget->addWidget(g);
    ui->stackedWidget->insertWidget(0,new QWidget(this));
    QWidget* kp  = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout(kp);
    QListView* k = new QListView(kp);
    k->setItemDelegate(new RefundResultModel::OrderDelegate());
    k->setObjectName("listview");
    layout->addWidget(k);
    kp->setLayout(layout);
    ui->stackedWidget->addWidget(kp);
    ui->stackedWidget->addWidget(new FriendUserPage(this));

}
QTreeWidgetItem* frm_UserPage::getItem(QTreeWidgetItem* par,QString f){
    for(int i=0;i<par->childCount();i++){
        if(par->child(i)->text(0)==f)return par->child(i);
    }
    QTreeWidgetItem* t = new QTreeWidgetItem(frm_UserPage::itGroupitem);
    t->setText(0,f);
    par->addChild(t);
    return t;
}
void frm_UserPage::setUser(User t){
    PersonalCenter* f = dynamic_cast<PersonalCenter*>(ui->stackedWidget->widget(2));
    f->setUser(t);
}
void frm_UserPage::itemclicked(QTreeWidgetItem *cur, int column){
    QString k = cur->text(0);
    if(k=="个人信息"){
        ui->stackedWidget->setCurrentIndex(2);
    }else if(k=="订单中心"){
        ui->stackedWidget->setCurrentIndex(1);
        emit RequestQueryModel();
    }else if(k=="乘客列表"){
        ui->stackedWidget->setCurrentIndex(1);
        emit RequestUserModel();
    }
}
void frm_UserPage::OrderRequestDone(QueryResultModel *fl){
    QWidget* p = ui->stackedWidget->widget(3);
    QListView* l = p->findChild<QListView*>("listview");
    l->setModel(fl);
    ui->stackedWidget->setCurrentIndex(3);
}
void frm_UserPage::UserModelRequestDone(UserModel* f){
    FriendUserPage* p = dynamic_cast<FriendUserPage*>(ui->stackedWidget->widget(4));
    p->setResult(f);
    ui->stackedWidget->setCurrentIndex(4);
}
